package com.see.driver;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import com.see.exception.ProductExistsException;
import com.see.server.ServerCore;
import com.see.servise.SEEServise;

public class Driver {
	private final String CONFIG_FILE="conf";
	private static Logger logger = LoggerFactory.getLogger(Driver.class);
	private int port;
	public SEEServise seeServise;

	public Driver() {
		seeServise = SEEServise.getInstance();
		ReadFromConfigurationFile();
		seeServise.start();
	}

	private void ReadFromConfigurationFile() {
		int count=0;
		try {
			BufferedReader in = new BufferedReader(new FileReader(CONFIG_FILE));
			port =Integer.parseInt(in.readLine());
			seeServise.setServerCore(new ServerCore(seeServise ,port));
			count=Integer.parseInt(in.readLine());
			for(int i=0;i<count;i++){
				String product=in.readLine();
				try {
					seeServise.createProductBook(product);
				} catch (ProductExistsException e) {
					logger.error(e.getMessage());
					e.printStackTrace();
				}
			}
			in.close();
		} catch (NumberFormatException | IOException e) {
			e.printStackTrace();
		}

	}
	
	
	public static void main(String[] args) throws InterruptedException {
		Driver driver = new Driver();
		driver.seeServise.getServerCore().init();

		//driver.serverCore.close();
	}
	
}
